<template>
    <div class="notice-carousel">
      <div class="carousel-container">
        <div ref="carouselItem" class="carousel-item">
          <i class="fas fa-bell"></i>
          <div class="notice-text" :style="{ transform: `translateX(-${currentIndex * 100}%)` }">
            <span v-for="(notice, index) in notices" :key="index" class="single-notice" :class="{ 'active': currentIndex === index }">
              {{ notice }}
            </span>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script>
  export default {
    props: {
      notices: {
        type: Array,
        required: true,
      },
    },
    data() {
      return {
        currentIndex: 0,
        timer: null,
      };
    },
    mounted() {
      this.startCarousel();
    },
    beforeDestroy() {
      clearInterval(this.timer);
    },
    methods: {
      startCarousel() {
        this.timer = setInterval(() => {
          this.currentIndex = (this.currentIndex + 1) % this.notices.length;
        }, 3000); // 每3秒切换一次
      },
    },
  };
  </script>
  
  <style scoped>
  .notice-carousel {
    position: relative;
    height: 50px; /* 根据需要调整 */
    overflow: hidden;
  }
  
  .carousel-container {
    position: relative;
    width: 100%;
    height: 100%;
  }
  
  .carousel-item {
    display: flex;
    align-items: center;
    height: 100%;
  }
  
  .fa-bell {
    margin-right: 10px;
  }
  
  .notice-text {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    white-space: nowrap;
  }
  
  .single-notice {
    display: inline-block;
    width: 100%;
    transition: opacity 0.5s;
    opacity: 0;
  }
  
  .single-notice.active {
    opacity: 1;
  }
  </style>
  